<%@page pageEncoding="utf-8" contentType="text/html; charset=utf-8" %>
<%@ include file="/app/include/init_app.jsp" %>
<%
	String os				= StringUtil.nvl(request.getAttribute("os"));
	String book_code		= StringUtil.nvl(request.getParameter("book_code"));

	
	HashList bookInfo 		= (HashList)request.getAttribute("bookInfo");	
	
	
%>

<!DOCTYPE html>
<html class="ui-mobile">
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
	<title><%= HOME_NAME %></title>
	<link rel="stylesheet" href="../../css/mobile.app.css"/>
	<link rel="stylesheet" href="../../css/jquery.mobile-1.4.2.min.css"/>
	<script src="../../../js/jquery-1.7.2.min.js"></script>
	
	
	<script type="text/javascript">

	$(document).ready(function() {
		$('.ui-btn').on('touchstart', function() {
			$(this).addClass('ui-btn-active');
		});
		
		
		$('div.rate span').mouseover(function(){
			var curTitle = $(this).attr('title');
			$('div.rate span').each(function(){
				if ($(this).attr('title') <= curTitle) {
					$(this).text('★');
				} else {
					$(this).text('☆');
				}
			});
		});
		
		$('textarea').on('keyup', function(){
			this.style.height = "1px";
			this.style.height = (20+this.scrollHeight)+"px";
		});
		
		$('.modal-content div.btn').on('touchstart', function() {
			$(this).addClass('modal-btn-active');
		});
		
		$('#review_list').load('./bookDetail.app?book_code=<%=book_code%>&job_type=review', '', function() {});

	});


	function fnPurchaseBook() {
		if (<%=isLogin%> == false) {
			alert('로그인 후 구매하세요');
			return;
		}
		$('#saveForm').attr('action', './bookPurchase.do');
		$('#saveForm').submit();
	}

	
	function btnReviewSave() 
	{
		if (<%=isLogin%> == false) {
			cfConfirmModal('로그인하시겠습니까?');
			$('.ui-btn').removeClass('ui-btn-active');
			return false;
		}
		
		var intRate = 0;
		$('div.rate span').each(function(){
			if ($(this).text() == '★') {
				if (this.title > intRate) {
					intRate = this.title;
				}
			}
		});

		if (intRate == 0) {
			cfAlertModal('평점을 선택하세요');
			$('.ui-btn').removeClass('ui-btn-active');
			return false;
		} 
		

		$.ajax({
			url: '/home/store/reviewSave.do',
			type: "POST",
		    dataType: 'json',
		    data:{ 
		    	book_code:'<%=book_code%>',
		    	rate:intRate, 
		    	review_info:$('#review_info').val() 
		    },
		    error: function(data, status, err){
		    	cfAlertModal(err);
		    },
			success: function(data){
				if (data.result) {
					$('#reviewForm').remove();
					$('#review_message').show();
					$('#review_list').load('./bookDetail.app?book_code=<%=book_code%>&job_type=review', '', function() {});
				} else {
					cfAlertModal(data.message);
				}
		    }
		});
		
		$('.ui-btn').removeClass('ui-btn-active');
	}
	
	function cfAlertModal(msg) {
		cfOpenModal('alert', msg);
	}
	
	function cfConfirmModal(msg) {
		cfOpenModal('confirm', msg);
	}
	
	function cfOpenModal(ty, message) 
	{
		var winH = $(window).height();
	   	var winW = $(window).width();

	    //Set the popup window to center
	    $('.modal-content').css('top', 	winH/2 + $(document).scrollTop() - $('#modal-'+ty).height()/2);
	    $('.modal-content').css('left', winW/2 - $('#modal-'+ty).width()/2);
	    
	    $('#modal-background').show();
	    $('#modal-'+ty+' div.message').text(message);
		$('#modal-'+ty).show();
	}
	
	function cfCloseModal() {
		$('.modal-content').hide(); 
		$('#modal-background').hide();
		$('.modal-content .btn').removeClass('modal-btn-active');
	}
	
	

	//무료 다운로드
	function fnDownloadFree() 
	{
		$('#download_button').hide();
		$('#download').show();
	
<%	if (os.equals("android")) {	%>
		window.android.downloadFree('<%=book_code%>');
<%	} else if (os.equals("ios")) {	%>
		window.location="jscall:downloadFree:<%=book_code%>";
<%	}	%>

		
	}
	
	//샘플 다운로드 
	function fnDownloadSample() {
<%	if (os.equals("android")) {	%>
		window.android.downloadPreview('<%=book_code%>');
<%	} else if (os.equals("ios")) {	%>
		window.location="jscall:downloadPreview:<%=book_code%>";
<%	}	%>
	}
	
	
	//앱에서 콜백
	function callJS(progress) {
		if (progress == 100) {	//다운로드완료
			$('#progressbar').val(progress);
			$('#download_percent').text(progress+'%');
			$('#download_status').text('설치 중...');
		} else if (progress == 200){	//설치완료
			$('#download_status').text('설치완료');
			$('#download_percent').text('');
		} else {
			$('#progressbar').val(progress);
			$('#download_percent').text(progress+'%');
		}
	}

	</script>
</head>
<body class="ui-mobile-viewport ui-overlay-a">
    <div class="ui-page ui-page-theme-a ui-page-active">
    
        <div class="content">
        	
	        <div>
		        <div style="float:left; width:35%">
		        	<img src="<%= bookInfo.get("book_image_url",0) %>" style="width:100%"/>
		        </div>
	        	<div style="float:left; width:65%;">
	        		<div class="book_title"><%= bookInfo.get("book_name", 0) %></div>
	        		<div class="book_info">
		        		<p><%= bookInfo.get("author_name",0) %> 저</p>
						<p><%= bookInfo.get("publisher_name",0) %>, <%= StringUtil.toDate(bookInfo.get("publish_dt",0),".") %></p>
						<p>총 <%= StringUtil.toPrice(bookInfo.get("page_count",0)) %> page, <%= bookInfo.get("file_size", 0) %></p>
				<%	if ("0".equals(bookInfo.get("book_type",0))) { %>
						<div id="download">
							<progress id="progressbar" value="0" max="100"></progress>
							<div id="download_status"  >다운로드 중...</div>
							<div id="download_percent" ></div>
						</div>
						<div id="download_button" class="ui-btn ui-input-btn  ui-corner-all ui-shadow">무료 다운로드<input type="button" alt="다운로드" value="다운로드" onclick="fnDownloadFree()"/></div>
				<%	} else { %>
						<%-- <p><strong>판매가 :</strong> <span class="price"><%= bookInfo.getPrice("price",0) %></span>원</p> --%>
						<div class="ui-btn ui-input-btn ui-btn-active ui-corner-all ui-shadow">
							<%= bookInfo.getPrice("price",0) %>원 구매하기
							<input type="button" alt="구매하기" value="구매하기" onclick="fnPurchaseBook()"/>
						</div>
				<%	} %>
				
				<%	if (!"".equals(bookInfo.get("preview_book_url",0))) { %>
						<div class="ui-btn ui-input-btn ui-corner-all ui-shadow">샘플 다운로드 <span class="remark">(<%=bookInfo.get("preview_file_size",0) %>)</span><input type="button" alt="다운로드" value="다운로드" onclick="fnDownloadPreview()"/></div>
				<%	} %>
					</div>
				</div>
			</div>
			
			<div class="clear">

			</div>
			
			<div class="item_title mt30"><h3>설명</h3></div>
			<div class="item_desc"><%= StringUtil.toHTML(bookInfo.get("intro", 0)) %></div>

	<%	if (!"".equals(bookInfo.get("index_info", 0))) { %>
			<div class="item_title"><h3>목차</h3></div>
			<div class="item_desc"><%= StringUtil.toHTML(bookInfo.get("index_info", 0)) %></div>
	<%	} %>

	<%	if (!"".equals(bookInfo.get("author_info", 0))) { %>
			<div class="item_title"><h3>저자 정보</h3></div>
			<div class="item_desc"><%= StringUtil.toHTML(bookInfo.get("author_info", 0)) %></div>
	<%	} %>
			
	
		</div>
        
        <div id="modal-background"></div>
		<div id="modal-confirm" class="modal-content" >
			<div class="message"></div>
			<div class="btn" onclick="cfCloseModal()">취소</div>
			<div class="btn">확인</div>
		</div>
		
		<div id="modal-alert" class="modal-content" >
			<div class="message"></div>
			<div class="btn" onclick="cfCloseModal()">확인</div>
		</div>
        
	</div>
	
	
</body>
</html>


